const { Order,Goods } = require("../models.js");
const Router = require('koa-router');
const { Op } = require('sequelize');
const jwt = require('jsonwebtoken');
let router = new Router({
    prefix:'/order'
})


//判断用户是否登录
router.post('/islogin',async(ctx)=>{
	ctx.body = {
		code:200,
		msg:"已经登录成功"
	}
})



//加入购物车
router.post('/cart',async(ctx)=>{
	let jwts = jwt.verify(ctx.request.body.user_id,'zouling');
    let userid = jwts.openid;
    let goodspid = ctx.request.body.goods_pid;
    var goodscount = ctx.request.body.goods_count;
    let usergoods = await Order.findAll({
        attributes:['goods_count'],
        where:{
			[Op.and]:[
				{user_id:userid},
				{goods_pid:goodspid}
			]
        }
    })

    if(usergoods.length > 0){
        let number = usergoods[0].goods_count + parseInt(goodscount);
        await Order.update(
            { goods_count: number },
            {
                where:{
                    user_id:userid
                }
            }
        ).then(res=>{
            ctx.body = {
                code:200,
                msg:"加入购物车成功"
            }
        })
    }else{
        await Order.create({
            user_id:userid,
            goods_pid:goodspid,
            goods_count:goodscount,
            checked:'false'
        }).then(res=>{
            ctx.body = {
                code:200,
                msg:"加入购物车成功"
            }
        })
    }
})


//获取购物车商品
router.post('/getcart',async(ctx)=>{
	let jwts = jwt.verify(ctx.request.body.user_id,'zouling');
    let cartId = jwts.openid;
    try {
        await Order.findAll({
            attributes:[
                'user_id',
                'goods_pid',
                'goods_count',
                'checked'
            ],
            include:[
                {
                    model:Goods
                }
            ],
            where:{ user_id:cartId }
        }).then(res=>{
            let msg = [];
            res.forEach((val,index) => {
                msg[index] = {
                    'user_id':val.user_id,
                    'goods_pid':val.goods_pid,
                    'goods_count':val.goods_count,
                    'checked':val.checked,
                    'PID':val.good.PID,
                    'NAME':val.good.NAME,
                    'PRESENT_PRICE':val.good.PRESENT_PRICE,
                    'IMAGE1':val.good.IMAGE1
                }
            });
            ctx.body={
                code:200,
                msg:msg
            }
        })
    } catch (error) {
        ctx.body={
            code:400,
            msg:error
        }
    }
})


//删除所有购物商品
router.post('/delete',async(ctx)=>{
    let GoodsPid = ctx.request.body.goodpid;
    await Order.destroy({
        where:{
            goods_pid:{
               [Op.in]:GoodsPid
            }
        }
    }).then(res=>{
        ctx.body={
            code:200,
            msg:res
        }
    }).catch(error=>{
        console.log(error)
    })
})


//删除当前一个购物商品
router.post('/deleteone',async(ctx)=>{
    let GoodsPid = ctx.request.body.goodpid;
    await Order.destroy({
        where:{
            goods_pid:GoodsPid
        }
    }).then(res=>{
        ctx.body={
            code:200,
            msg:res
        }
    }).catch(error=>{
        console.log(error)
    })
})


module.exports = router;